#include "pacman.h"
#include <stdio.h>

void pacman_init(Pacman* pacman, int startRow, int startCol) {
    pacman->row = startRow;
    pacman->col = startCol;
    pacman->direction = DIR_RIGHT;
    pacman->score = 0;
    pacman->lives = 3;
}

void pacman_move(Pacman* pacman, Map* map, Direction dir) {
    int newRow = pacman->row;
    int newCol = pacman->col;
    
    // 根据方向计算新位置
    switch (dir) {
        case DIR_UP:
            newRow--;
            break;
        case DIR_DOWN:
            newRow++;
            break;
        case DIR_LEFT:
            newCol--;
            break;
        case DIR_RIGHT:
            newCol++;
            break;
    }
    
    // 检查新位置是否有效
    if (map_is_valid_position(map, newRow, newCol)) {
        // 更新地图
        map->grid[pacman->row][pacman->col] = EMPTY;
        
        // 检查是否吃到豆子
        if (map_eat_dot(map, newRow, newCol)) {
            pacman->score += 10;
        }
        
        // 更新Pacman位置
        pacman->row = newRow;
        pacman->col = newCol;
        pacman->direction = dir;
        
        // 在地图上绘制Pacman
        map->grid[pacman->row][pacman->col] = PACMAN;
    }
}

void pacman_draw(Pacman* pacman, Map* map) {
    // 在地图上绘制Pacman
    map->grid[pacman->row][pacman->col] = PACMAN;
}

int pacman_is_alive(Pacman* pacman) {
    return pacman->lives > 0;
} 